const _ = require('underscore');
const Util = require('./util');
import {Platform} from 'react-native';

const Common = {
  INIT: "1B 40",//初始化
  GO_BACK: "1B 6A 50",//退格 50 是16进制
  ENTER: '0D', //换行
  ALIGN_LEFT: "1B 61 00",//左对齐
  ALIGN_RIGHT: "1B 61 02",//居右对齐
  ALIGN_CENTER: "1B 61 01",//居中对齐

  UNDER_LINE: "1C 2D 01",//下划线

  PRINT_AND_NEW_LINE: "0A",//打印并换行

  FONT_SMALL: "1B 4D 01",//小号字体 9x17
  FONT_BIG_SMALL: "1B 4D 10",//小号字体 9x17
  FONT_NORMAL: "1C 4D 00",//正常 12x24
  //FONT_NORMAL: "1C 59 27 20 00",//正常 12x24
  FONT_BOLD: "1B 45 01",//粗体

  FONT_HEIGHT_TIMES: '1B 21 10',
  FONT_WIDTH_TIMES: '1B 21 20',
  FONT_HEIGHT_WIDTH_TIMES: '1B 21 30',

  SOUND: "1B 42 02 02", // 蜂鸣 2次/100ms
  PAGE_LENGTH: "1B 43 21",//设置页面长度
  PAGE_NEXT: "0C", //换页
  PAGE_BLANK: "1B 4E 01",//设定页缝空白
  DEFAULT_LINE_L: "1B 32", // 设定默认行间距
  LINE_L: "1B 33 10", // 设定行间距  原来值 1B 33 10

  BEI_HEIGHT: '1B 4D 30',

  FONT_LOCK_BIG_CHINESE: "1C 57 01",//汉字倍宽打印
  FONT_UNLOCK_BIG_CHINESE: "1C 57 00",//解除汉字倍宽打印

  FONT_LOCK_BIG_ENGLISH: "1B 57 01",//英文倍宽打印
  FONT_UNLOCK_BIG_ENGLISH: "1B 57 00",//解除英文倍宽打印

  FONT_LINE_TABLE_LOCK: "1C 76 01",//中文表格连线
  FONT_LINE_TABLE_UNLOCK: "1C 76 00",//解除中文表格连线

  FONT_CHINESE_LOCK_FAST: "1C 78 01",//汉字高速打印
  FONT_CHINESE_UNLOCK_FAST: "1C 78 00",//解除汉字高速打印


  QRCODE_MODEL: "1D 28 6B 03 00 31 43 05",//设置二维码模块高度
  QRCODE_DATA: "1D 28 6B 0B 00 31 50 30 47 70 72 69 6E 74 65 72",//二维码数据
  QRCODE_PRINT: "1D 28 6B 03 00 31 51 30",//打印二维码

};

const Config = {
  wordNumber: 48 // 可打印的字数，对应80mm纸张
};

let appendDataWithString;
let writeTextToDevice, writeHexToDevice;

function _setBT(bt) {
  appendDataWithString = bt.appendDataWithString;
  writeTextToDevice = bt.writeTextToDevice;
  writeHexToDevice = bt.writeHexToDevice;
}

function setConfig(config) {
  Object.assign(Config, config);
}

function leftRight(left, right, wordNumber = Config.wordNumber) {
  return left + Util.getSpace(wordNumber - Util.getWordsLength(left) - Util.getWordsLength(right)) + right;
}

function keyValue(name, value, wordNumber = Config.wordNumber) {
  const nameLen = Util.getWordsLength(name);
  let vArr = [], temp = '';
  _.each(value, (v, i) => {
    const tvLen = Util.getWordsLength(temp + v);
    const diff = tvLen - (wordNumber - nameLen);
    if (diff <= 0) {
      temp += v;
      if (i === value.length - 1) {
        vArr.push(temp);
      }
    } else {
      if (Util.isChinese(v) && diff === 1) {
        temp += ' ';
      }
      vArr.push(temp);
      temp = v;
    }
  });
  return _.map(vArr, (v, i) => {
    if (i === 0) {
      return name + v;
    } else {
      return Util.getSpace(name.length) + v;
    }
  }).join('');
}

const ESC = {
  Common,
  Util: {
    leftRight,
    keyValue,
  },
  _setBT,
  setConfig,

  init(time = 0) {
    appendDataWithString(Common.INIT, time);
  },
  test(string ,time = 0){
    appendDataWithString(string, time);
  },
  setGoBack(time = 0) {
    appendDataWithString(Common.GO_BACK, time);
  },
  setEnter(time = 0) {
    appendDataWithString(Common.ENTER, time);
  },
  printAndNewLine(time = 0) {
    appendDataWithString(Common.PRINT_AND_NEW_LINE, time);
  },
  alignLeft(time = 0) {
    appendDataWithString(Common.ALIGN_LEFT, time);
  },
  alignCenter(time = 0) {
    appendDataWithString(Common.ALIGN_CENTER, time);
  },
  setPageLength(time = 0) {
    appendDataWithString(Common.PAGE_LENGTH, time);
  },
  setPageNext(time = 0) {
    appendDataWithString(Common.PAGE_NEXT, time);
  },
  setPageBlank(time = 0) {
    appendDataWithString(Common.PAGE_BLANK, time);
  },
  setLineL(time = 0) {
    appendDataWithString(Common.LINE_L, time);
  },
  setDefaultLineL(time = 0) {
    appendDataWithString(Common.DEFAULT_LINE_L, time);
  },
  alignRight(time = 0) {
    appendDataWithString(Common.ALIGN_RIGHT, time);
  },

  underline(time = 0) {
    appendDataWithString(Common.UNDER_LINE, time);
  },
  fontSmall(time = 0) {

    appendDataWithString(Common.FONT_SMALL, time);

  },
  fontNormal(time = 0) {

    appendDataWithString(Common.FONT_NORMAL, time);

  },
  fontLockBigChinese(time = 0) {

    appendDataWithString(Common.FONT_LOCK_BIG_CHINESE, time);

  },
  fontUnlockBigChinese(time = 0) {

    appendDataWithString(Common.FONT_UNLOCK_BIG_CHINESE, time);

  },
  fontLockBigEnglish(time = 0) {

    appendDataWithString(Common.FONT_LOCK_BIG_ENGLISH, time);

  },
  fontUnlockBigEnglish(time = 0) {

    appendDataWithString(Common.FONT_UNLOCK_BIG_ENGLISH, time);

  },
  fontBig(time = 0) {

    appendDataWithString(Common.BEI_HEIGHT, time);

  },
  fontLockTableLine(time = 0) {

    appendDataWithString(Common.FONT_LINE_TABLE_LOCK, time);

  },
  fontUnlockTableLine(time = 0) {

    appendDataWithString(Common.FONT_LINE_TABLE_UNLOCK, time);

  },
  fontBold(time = 0) {
    appendDataWithString(Common.FONT_BOLD, time);
  },

  fontHeightTimes(time = 0) {
    appendDataWithString(Common.FONT_HEIGHT_TIMES, time);
  },
  fontHeightTimes(time = 0) {
    appendDataWithString(Common.FONT_WIDTH_TIMES, time);
  },
  fontHeightTimes(time = 0) {
    appendDataWithString(Common.FONT_HEIGHT_WIDTH_TIMES, time);
  },
  fontLockChineseFast(time = 0) {
    appendDataWithString(Common.FONT_CHINESE_LOCK_FAST, time);
  },
  fontUnlockChineseFast(time = 0) {
    appendDataWithString(Common.FONT_CHINESE_UNLOCK_FAST, time);
  },
  setQrcodeHeight(time = 0) {
    appendDataWithString(Common.QRCODE_MODEL, time);
  },
  setQrcodeData(time = 0) {
    appendDataWithString(Common.QRCODE_DATA, time);
  },
  printQrcode(time = 0) {

    appendDataWithString(Common.QRCODE_PRINT, time);
  },

  text(str, time = 1) {

    appendDataWithString(str, time);

  },
  sound() {

    appendDataWithString(Common.SOUND, 0);

  }
};

module.exports = ESC;
